﻿using FluentValidation;

using Microsoft.AspNetCore.Rewrite;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using ZFB.Models;

namespace ZFB.DTO.FluentValidation
{
    public class AddAdminUserValidator:AbstractValidator<AddAdminUserDTO>
    {
        public AddAdminUserValidator() 
        {
            RuleFor(e => e.phoneNum).NotEmpty().WithMessage("手机号不能为空").Must(IsMobile).WithMessage("手机号格式不正确");
            RuleFor(x =>x.password ).NotEmpty().WithMessage("密码不能为空").Length(6, 16).WithMessage("密码长度至少6个字符，最多16个字符").Must(EncryptionPassword).WithMessage("密码不符合规则,必须包含数字、小写或大写字母、特殊符号");
            RuleFor(x => x.name).NotEmpty().WithMessage("名称不允许为空").Length(2,6).WithMessage("名称长度至少2个字符，最多6个字符");
            RuleFor(x => x.email).NotEmpty().WithMessage("邮箱不允许为空").EmailAddress().WithMessage("不是有效的邮箱");



        }
        private bool EncryptionPassword(string password)
        {
            //正则
            var regex = new Regex(@"
                            (?=.*[0-9]) 
                            (?=.*[a-zA-Z])  
                            (?=([\x21-\x7e]+)[^a-zA-Z0-9])  
                            .{6,16}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
            return regex.IsMatch(password);
        }
        private bool IsMobile(string arg)
        {
            return Regex.IsMatch(arg, @"^[1][3-8]\d{9}$");
        }

    }
}
